package com.youuvs.org.controller;

import com.youuvs.basic.utils.JsonResult;
import com.youuvs.basic.utils.PageList;
import com.youuvs.org.domian.Employee;
import com.youuvs.org.query.EmployeeQuery;
import com.youuvs.org.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;
    @GetMapping
    public List<Employee> findAll(){
        List<Employee> lists=employeeService.findAll();
        return lists;
    }
    @GetMapping("/{id}")
    public Employee findById(@PathVariable("id") Long id){
        return employeeService.findById(id);
    }
    @DeleteMapping("/{id}")
    public JsonResult delete(@PathVariable("id") Long id){
        try {
            employeeService.delete(id);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult().me();
        }
    }

    @PutMapping
    public JsonResult insertOrUpdate(@RequestBody Employee employee){
        try {
            if(employee.getId()==null){
                employeeService.insert(employee);
            }else{
                employeeService.update(employee);
            }
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult().me();
        }
    }

   /* @PutMapping
    public JsonResult update(@RequestBody Employee employee){
        try {
            employeeService.update(employee);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult().me();
        }
    }*/

    @PostMapping
    public PageList<Employee> pageQuery(@RequestBody EmployeeQuery query){
        try {
            PageList<Employee> lists=employeeService.queryPage(query);
           return lists;
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<>();
        }
    }

    @PatchMapping
    //@RequestBody 将参数放进请求体中
    public JsonResult patchDel(@RequestBody List<Long> ids){

        try {
            employeeService.patchDel(ids);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult().me();
        }

    }


}
